/**
 * Created by sunyx on 2016/9/29.
 */
import React, { PropTypes } from 'react';
import FormModal from '../../components/FormModal';
import {Form, Input} from 'antd';
import {post} from '../../logics/rpc';
import md5 from "../../logics/md5";
import alert from '../../utils/alert';

@Form.create()
export default class AddGoodModal extends React.Component {
  onReturn = ev => {
    this.refs.form.submit();
  };
  fields = {
    name:  {
      rules: [
        { required: true, message: '商品名' },
      ],
    },
    price: {
      rules: [
        { required: true, message: '价格' },
      ],
    },
    cover: {
      rules: [
        { required: true, message: '商品图片' },
      ],
    },
    content: {
      rules: [
        { required: true, message: '商品一句话描述' },
      ],
    },
    description: {
      rules: [
        { required: true, message: '商品详细描述' },
      ],
    },
    property:{
      rules:[
        {required:true,message:'商品性质'},
      ],
    },
    advertiseCount:{
      rules:[
        {required:true,message:'广告总数'},
      ],
    },
    soldout:{
      rules:[
        {required:true,message:'销量'},
      ],
    },
    channelArguments:{
      rules:[
        {required:true,message:'微店参数'},
      ],
    },
  };
  submit = async ({name,price,cover,content,description,property,advertiseCount,soldout,channelArguments}) => {
    await post('/admin/good/create', {
    name,price,cover,content,description,property,advertiseCount,soldout,channelArguments
    });
    alert('创建成功');
    const {onAdded} = this.props;
    onAdded && onAdded();
  };
  render() {
    const { fields } = this;
    const { getFieldProps } = this.props.form;
    return (
      <FormModal
        ref="form"
        title="添加商品"
        okTitle="确 定"
        onSubmit={this.submit}
        {...this.props}
      >
        <Form.Item label="商品名">
          <Input {...getFieldProps('name', fields.name)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="价格">
          <Input {...getFieldProps('price', fields.price)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="商品图片">
          <Input {...getFieldProps('cover', fields.cover)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="商品一句话描述">
          <Input {...getFieldProps('content', fields.content)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="商品详细描述">
          <Input {...getFieldProps('description', fields.description)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="商品性质">
          <Input {...getFieldProps('property', fields.property)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="广告总数">
          <Input {...getFieldProps('advertiseCount', fields.advertiseCount)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="销量">
          <Input {...getFieldProps('soldout', fields.soldout)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="微店参数">
          <Input {...getFieldProps('channelArguments', fields.channelArguments)} onPressEnter={this.onReturn} />
        </Form.Item>
      </FormModal>
    );
  }
}
